package com.ant.backstage.beanFactory;

import com.ant.backstage.facade.dto.MailTempLetDTO;
import com.ant.backstage.facade.request.MailRequest;
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * @Author zhangyu
 * @Description
 * @Date 11:36 2021/12/28
 * @Param 继承Orika   Bean拷贝基础工厂，可直接在Service中Autowired MapperFacade使用拷贝功能
 * @return
 **/
@Component
public class MapperFacadeFactory implements FactoryBean<MapperFacade> {
    @Override
    public MapperFacade getObject(){
        return this.init().getMapperFacade();
    }

    @Override
    public Class<?> getObjectType() {
        return MapperFacade.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    private MapperFactory init() {
        MapperFactory factory = new DefaultMapperFactory.Builder().build();
        //处理特殊类型转换 自定义 exclude 排除User.class的字段  field字段匹配
        /*
         * 邮件入参转换
         */
        factory.classMap(MailRequest.class, MailTempLetDTO.class).exclude("acceptList").exclude("copyList")
                .exclude("bccList").exclude("attachmenturl").exclude("imgUrl").customize(new CustomMapper<MailRequest, MailTempLetDTO>() {
                    //MappingContext context 必须   void mapAtoB(A var1, B var2, MappingContext var3);
            @Override
            public void mapAtoB(MailRequest mailRequest,MailTempLetDTO mailTempLetDTO,MappingContext context) {
                if (StringUtils.isNotBlank(mailRequest.getAcceptList())) {
                    if (mailRequest.getAcceptList().indexOf(",") > -1) {
                        mailTempLetDTO.setAcceptList(mailRequest.getAcceptList().split(","));
                    }else {
                        String  temp[]={mailRequest.getAcceptList()};
                        mailTempLetDTO.setAcceptList(temp);
                    }
                }
                if (StringUtils.isNotBlank(mailRequest.getBccList())) {
                    if (mailRequest.getBccList().indexOf(",") > -1) {
                        mailTempLetDTO.setBccList(mailRequest.getBccList().split(","));
                    }else {
                        String  temp[]={mailRequest.getBccList()};
                        mailTempLetDTO.setBccList(temp);
                    }
                }
                if (StringUtils.isNotBlank(mailRequest.getCopyList())) {
                    if (mailRequest.getCopyList().indexOf(",") > -1) {
                        mailTempLetDTO.setCopyList(mailRequest.getCopyList().split(","));
                    }else {
                        String  temp[]={mailRequest.getCopyList()};
                        mailTempLetDTO.setCopyList(temp);
                    }
                }
                if (StringUtils.isNotBlank(mailRequest.getAttachmenturl())) {
                    if (mailRequest.getAttachmenturl().indexOf(",") > -1) {
                        mailTempLetDTO.setAttachmenturl(mailRequest.getAttachmenturl().split(","));
                    }else {
                        String  temp[]={mailRequest.getAttachmenturl()};
                        mailTempLetDTO.setAttachmenturl(temp);
                    }
                }
                if (StringUtils.isNotBlank(mailRequest.getImgUrl())) {
                    if (mailRequest.getImgUrl().indexOf(",") > -1) {
                        mailTempLetDTO.setImgUrl(mailRequest.getImgUrl().split(","));
                    }else {
                        String  temp[]={mailRequest.getImgUrl()};
                        mailTempLetDTO.setImgUrl(temp);
                    }
                }
            }
        }).byDefault().register();


        return factory;
    }

}
